package com.zwb.config;

import com.zwb.service.common.MailConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author 头发又黑又长
 * @Date 2022/9/9 9:15
 * @email zwb15083976291@163.com
 */
@Slf4j
@Configuration
public class RabbitMQConfig {


    /* 交换机 */
    @Bean
    public DirectExchange emailExchange() {
        return ExchangeBuilder
                .directExchange(MailConstants.EXCHANGE_EMAIL)
                .durable(true).build();
    }

    /* 注册队列 */
    @Bean
    public Queue emailQueue() {
        /* 队列延迟1分钟执行 */
        return QueueBuilder.durable(MailConstants.QUEUE_EMAIL)
                .ttl((MailConstants.MSG_TIMEOUT * 60000))
                /* 绑定延迟队列 */
                .deadLetterExchange(MailConstants.DELAY_EXCHANGE)
                .deadLetterRoutingKey(MailConstants.DELAY_EMAIL_ROUTING_KEY)
                .build();
    }

    /* 交换机绑定 */
    @Bean
    public Binding emailBinding() {
        return BindingBuilder.bind(emailQueue()).to(emailExchange()).with(MailConstants.EMAIL_ROUTING_KEY);
    }


}
